local RoomBase = require 'packages.hearthstone.battlegrounds.core.roombase'
local ClientPlayer = require "packages.hearthstone.battlegrounds.client.clientplayer"

---@class Hearthstone.Client: Hearthstone.RoomBase, ClientBase
local Client = RoomBase:subclass("Hearthstone.Client")
Client:include(Fk.Base.ClientBase)

-- 手写泛型 这里帮你写了

---@class VoidGame.Client
---@field public players VoidGame.ClientPlayer[]
---@field public observers VoidGame.ClientPlayer[]
---@field public current VoidGame.ClientPlayer
---@field public getPlayerById fun(self: VoidGame.Client, id: integer): VoidGame.ClientPlayer
---@field public getPlayerBySeat fun(self: VoidGame.Client, seat: integer): VoidGame.ClientPlayer
---@field public setCurrent fun(self: VoidGame.Client, current: VoidGame.ClientPlayer)
---@field public getCurrent fun(self: VoidGame.Client): VoidGame.ClientPlayer

function Client:initialize(_client)
  RoomBase.initialize(self)
  Fk.Base.ClientBase.initialize(self, _client)

  self.clientplayer_klass = ClientPlayer

  -- 将服务端下发的指令透传给 QML（第三个参数为 true 表示继续传递到 UI）
  self:addCallback("Battlegrounds.UpdatePhase", self.onUpdatePhase, true)
  self:addCallback("Battlegrounds.RecruitPhase", self.onRecruitPhase, true)
  self:addCallback("Battlegrounds.CombatPhase", self.onCombatPhase, true)
  self:addCallback("Battlegrounds.CombatResult", self.onCombatResult, true)
end

-- 以下处理函数主要用于在需要时缓存本地状态；由于使用了 true 透传，UI 同时也会收到同名 command
function Client:onUpdatePhase(data)
  self._lastUpdatePhase = data
end

function Client:onRecruitPhase(data)
  -- 记录最近一次招募阶段数据，便于首次进入页面时同步
  self._lastRecruitPhase = data
end

function Client:onCombatPhase(data)
  self._lastCombatPhase = data
end

function Client:onCombatResult(data)
  self._lastCombatResult = data
end

function Client:sendDataToUI(data)
  -- 初次进入页面时，把最近一次的阶段数据同步给 QML（若存在）
  if self._lastUpdatePhase then
    self:notifyUI("Battlegrounds.UpdatePhase", self._lastUpdatePhase)
  end
  if self._lastRecruitPhase then
    self:notifyUI("Battlegrounds.RecruitPhase", self._lastRecruitPhase)
  end
  if self._lastCombatPhase then
    self:notifyUI("Battlegrounds.CombatPhase", self._lastCombatPhase)
  end
  if self._lastCombatResult then
    self:notifyUI("Battlegrounds.CombatResult", self._lastCombatResult)
  end
end

return Client

